package com.matt.controller.pad.care;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.Children;
import com.matt.domain.care.HLDailyRecord;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.vo.care.HLChildDailyRecordPageVO;
import com.matt.service.ChildrenService;
import com.matt.service.ClassesService;
import com.matt.service.care.HLDailyRecordService;
import com.matt.service.care.po.HLDailyRecordPO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.RequestConstant.TICKET;

;

@RestController
@RequestMapping("/pad/record/daily")
@Slf4j
@Api(tags = "护理记录 - 宝宝一日")
public class PadCareDailyController extends CareBaseController<HLDailyRecord, HLDailyRecordService, HLDailyRecordPO> {

    @Autowired
    ClassesService classesService;

    @Autowired
    ChildrenService childrenService;

    @Autowired
    public void setBaseService(HLDailyRecordService service){
        super.setBaseService(service);
    }


    @ApiOperation(value = "获取多宝宝的综合列表, 分页仅在classId时有用")
    @GetMapping("/multiple")
    public StatusResponse get(@RequestHeader(TICKET) String ticket, String classId, String[] childIds, Date daily, PagerQuery pagerQuery){
        StaffDTO staff = cache.getStaff(ticket);

        if (childIds == null && StringUtils.isNotEmpty(classId)){
            Page<Children> page = classesService.getChildIdsByClassesId(staff.getAreaId(), classId, pagerQuery);

            List<HLDailyRecord> records = baseService.getAllByChildIdsAndDaily(staff.getAreaId(), staff.getId(), page.getContent(), daily);

            Map<String, HLDailyRecord> cache = records.stream().collect(Collectors.toMap(HLDailyRecord::getChildId, o -> o));

            Page<HLChildDailyRecordPageVO> ret = page.map(o->
                HLChildDailyRecordPageVO.of(o, cache.get(o.getId()))
            );

            return StatusResponse.success(ret);
        }

        List<Children> cs = childrenService.getByIds(childIds, staff.getAreaId());

        List<HLDailyRecord> records = baseService.getAllByChildIdsAndDaily(staff.getAreaId(), staff.getId(), cs, daily);
        Map<String, HLDailyRecord> cache = records.stream().collect(Collectors.toMap(HLDailyRecord::getChildId, o -> o));

        List<HLChildDailyRecordPageVO> vos = cs.stream().map(o ->
                HLChildDailyRecordPageVO.of(o, cache.get(cache.get(o.getId())))
        ).collect(Collectors.toList());

        Page<HLChildDailyRecordPageVO> ret = new PageImpl<>(vos);
        return StatusResponse.success(ret);
    }

    @Override
    public StatusResponse add(@RequestHeader(TICKET) String ticket, HLDailyRecordPO param){
        return StatusResponse.error(ErrorCode.CANNOT_MODIFY_PLAN, "你应该用不到这个接口");
    }
}
